include Makefile.config

NDLL_DIR := ndll/$(ARCH_CAP)

LUA_NDLL=hxvmlua.ndll
LUA_SRC=src/hxvm_lua.cpp
LUA_OBJS=objs/ndll/hxvm_lua.o objs/ndll/hashtool.o

PYTHON_NDLL=hxvmpython.ndll
PYTHON_SRC=src/hxvm_python.cpp
PYTHON_OBJS=objs/ndll/hxvm_python.o objs/ndll/hashtool.o

NDLLS=$(LUA_NDLL) $(PYTHON_NDLL)

all: directories archtest $(NDLLS)

rebuild: clean all

directories:
	@mkdir -p objs/ndll
	@mkdir -p $(NDLL_DIR)

objs/ndll/hxvm_lua.o: src/hxvm_lua.cpp
	$(CPP) -shared $(FLAGS) $(INCPATH) $(DEFINES) -c $< -o $@

objs/ndll/hxvm_python.o: src/hxvm_python.cpp
	$(CPP) -shared $(FLAGS) $(INCPATH) $(DEFINES) -c $< -o $@

objs/ndll/hashtool.o: src/hashtool.cpp
	$(CPP) -shared $(FLAGS) $(INCPATH) $(DEFINES) -c $< -o $@

$(PYTHON_NDLL) : $(PYTHON_OBJS)
	$(CPP) -shared -O2 $(FLAGS) $(LIBPATH) -o $(NDLL_DIR)/$@ $(PYTHON_OBJS) $(LIBS) $(LIBS_PYTHON) -lm
	$(STRIP) --strip-unneeded $(NDLL_DIR)/$@

$(LUA_NDLL) : $(LUA_OBJS)
	$(CPP) -shared -O2 $(FLAGS) $(LIBPATH) -o $(NDLL_DIR)/$@ $(LUA_OBJS) $(LIBS) $(LIBS_LUA) -lm
	$(STRIP) --strip-unneeded $(NDLL_DIR)/$@

clean:
	@rm -f *.o
	@rm -f objs/ndll/*.o
	@rm -f *.ndll
	@rm -f test/test.n
	@rm -rf objs

distclean: clean
	find . -name "*~" -exec rm {} \;

ndll_clean: distclean
	@rm -f ndll/Linux/*
	@rm -f ndll/Linux64/*
	@rm -f ndll/Windows/*
	@rm -f ndll/Mac/*
	@rm -f ndll/BSD/*

archtest:
	@if [ -z $(ARCH_CAP) ]; then \
		echo "You must specify the architecture to build for (make ARCH=?): "; \
		echo " ARCH: linux linux64 windows mac bsd wine"; \
		exit 1; \
	fi;

install: allarchs
	cp ndll/Linux/hxvmlua.ndll ~/lib/
	cp ndll/Linux/hxvmpython.ndll ~/lib/
	cp ndll/Windows/hxvmlua.ndll ~/.wine/drive_c/neko/
	cp ndll/Windows/hxvmpython.ndll ~/.wine/drive_c/neko/

release: dummy allarchs
	make distclean
	make docs
	@rm -rf release
	@mkdir -p ./release/scripting
	@cp -r scripting release/scripting/
	@cp -r ndll release/scripting/
	@cp -r test release/scripting/
	@cp haxelib.xml release/scripting/
	@cp -r docs release/scripting/
	@find release -name "*.svn" -type d -print | xargs /bin/rm -rf
	cd release && zip -r scripting scripting
	rm -rf release/scripting
	haxelib test release/scripting.zip
	wine haxelib test release/scripting.zip
	cd test; haxe -neko test.n -lib scripting -main Test; neko test.n
	cd test; wine neko test.n
	@echo ""
	@echo "--------------------------------------"
	@echo "------ build and test complete -------"
	@echo "--------------------------------------"
	@echo "If all went well, you may now commit to haxelib with the command:"
	@echo "haxelib submit release/scripting.zip"

allarchs:
	make ARCH=linux rebuild
	make ARCH=windows rebuild

clean-docs: dummy
	@find docs -name "*.html" -type f -print | xargs /bin/rm -rf

docs: clean-docs
	haxe docs.hxml
	chxdoc -o docs --title="Neko Scripting" --subtitle="Extension Lib" --tmpDir=__chxdoctmp --includeOnly=scripting.* --ignoreRoot=true --templateDir=../../caffeine-hx/projects/chxdoc/templates/default --installTemplate=true --developer=false --generateTodoFile=true --showTodoTags=true neko.xml,neko
	$(RM) -R __chxdoctmp

dummy:
